<?php
/**
 * urCronServiceFlickrBase.
 * @package    urCronPlugin
 * @subpackage urCronServiceFlickrPlugin
 * @author     Marc Weistroff <marc.weistroff@gmail.com>
 * @version    $Id: urCronServiceFlickrBase.class.php 20 2009-06-10 15:38:21Z flambeur $
 *  
 */

/**
 * @abstract
 */
class urCronServiceFlickrBase extends urCronServiceBase
{
  protected function doCronJobAtom()
  {
  	$this->feed->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
    $this->feed->registerXPathNamespace('flickr', 'urn:flickr:');
    $this->feed->registerXPathNamespace('media', 'http://search.yahoo.com/mrss/');
 
    foreach($this->feed->entry as $entry)
    {
      if(Doctrine::getTable('urCronItem')->findOneByOriginalId($entry->id()))
        return;

      $this->object = new urCronItem();

      $data = $this->parseAtomEntry($entry);
      $this->object->synchronizeWithArray($data);
      $this->object->addTags($this->parseTags($entry));
      $this->object->addData('author_uri', (string)$entry->author->uri);
      $imageUri = $this->getPreviewImageUrl($this->object, $entry);
      
      $this->object->addData('image_medium_uri', $imageUri);
      $this->object->save();
    }
  }

  /**
   * Get the image preview
   * @param UrCronItem $item
   */
  protected function getPreviewImageUrl($item)
  {
    $content = $item->getOriginalContent();
    preg_match( "    /(http\:\/\/farm.+\.flickr.com.+)(\_.)(.[a-z]{3})/", $content, $matches);

    return $matches[1] . $matches[3];
  }

}
?>
